package cn.imvc.app.controllers.goods;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import cn.imvc.app.entities.AttrClass;
import cn.imvc.app.entities.AttrValue;
import cn.imvc.app.models.AttrClassModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author zhy
 * @date 2016年7月23日上午10:26:51
 *
 */
public class AttrClassControllerImpl implements AttrClassController{
   
	public Page<AttrClass> page;
	public ViewRequest viewRequest;
	public AttrClass attrClass;
	public int id;
	public int[] ids;
	private String search_content;
	public String attrCV;
	public AttrValue attrValue;
	static Logger logger = Logger.getLogger(AttrClassControllerImpl.class);
	@Autowired
	AttrClassModel attrClassModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		
		return null;
	}

	public String index() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getParams().remove("_and_attrClass_eq_number");
		viewRequest.getSort().put("sort", "ASC");
		  page = attrClassModel.find(viewRequest);
		  return "attr-class/index";
		
	}  
	
	public String show() {
		attrClass = attrClassModel.get(id);
		return "attr-class/edit";
	}

	public String editNew() {
		
		return "attr-class/edit";
	}
    
	public String create() {
		int result = attrClassModel.save(attrClass);
		   if(result ==0){
			 return Render.json(new DialogBean("300","属性编码不能重复","attr-class","closeCurrent"));
			}
			if(result ==-1){
			 return Render.json(new DialogBean("300","属性名称不能重复","attr-class","closeCurrent"));
			}
			loggingModel.info(logger,"添加属性："+attrClass.getName());
		return Render.json(new DialogBean("200","添加成功","","closeCurrent"));
	}

	public String edit() {
		attrClass = attrClassModel.get(id);
		return "attr-class/edit";
	}

	public String update() {
		int result = attrClassModel.update(id,attrClass);
		    if(result ==0){
			 return Render.json(new DialogBean("300","属性编码不能重复","attr-class","closeCurrent"));
			}
			if(result ==-1){
			 return Render.json(new DialogBean("300","属性名称不能重复","attr-class","closeCurrent"));
			}
			loggingModel.info(logger,"修改属性："+attrClass.getName());
	   return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}

	public String destroy() {
		String result = attrClassModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除属性："+id);
		    return Render.json(new DialogBean("200","删除成功","attr-class"));
		}else{
			return Render.json(new DialogBean("300",result,"attr-class"));
		}
	}

	public String destroyBatch() {
		attrClassModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功","attr-class"));
	}
	
	/**
	 * 外部调用
	 * @return
	 */
    public String dialog(){
    	if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content != null && !search_content.trim().equals("")){
			viewRequest.getParams().put("name_like", search_content);
		}
		page = attrClassModel.find(viewRequest);
		return "attr-class/dialog";
    	
    }
    /**
     * 商品类别调用
     * 
     */
   public String goodsClassDialog(){
    	if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		if(search_content != null && !search_content.trim().equals("")){
			viewRequest.getParams().put("code_like", search_content);
			viewRequest.getParams().put("_or_name_like", search_content);
			viewRequest.getParams().put("_or_sort_like", search_content);
		}
		page = attrClassModel.find(viewRequest);
		return "attr-class/goodsClassDialog";
    	
    }
	
	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public AttrClass getAttrClass() {
		return attrClass;
	}

	public void setAttrClass(AttrClass attrClass) {
		this.attrClass = attrClass;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public String getSearch_content() {
		return search_content;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}

	public String getAttrCV() {
		return attrCV;
	}

	public void setAttrCV(String attrCV) {
		this.attrCV = attrCV;
	}
   

}
